/*
 * Copyright (c) 2010. raoqn
 */

package com.yfeng.goodjuhome.service.impl;

import com.yfeng.common.dao.IDao;
import com.yfeng.common.dao.Param;
import com.yfeng.common.utils.DateUtil;
import com.yfeng.common.utils.Util;
import com.yfeng.exception.ServiceException;
import com.yfeng.goodjuhome.model.UserActivityModel;
import com.yfeng.goodjuhome.service.IUserActivityService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 记录用户在网站进行活动的服务类
 * www.goodjuhome.com
 * Created by IntelliJ IDEA.
 * User: rqn
 * Date: 2010-8-14
 * Time: 23:07:16
 */
@Service("userActivityService")
public class UserActivityServiceImpl implements IUserActivityService {

    private final Log log = LogFactory.getLog(this.getClass());
    @Autowired
    public IDao baseDao;


    public int addActivity(String uid, String activity) throws ServiceException {
        try {
            Param param = new Param();
            param.put("uid", uid);
            param.put("activity", activity);
            param.put("createtime", DateUtil.getCurDateTime());
            int ret = baseDao.save("user_activity", param);
            return ret;
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new ServiceException(e.getMessage(), e);
        }
    }

    public int deleteActivity(String[] ids) throws ServiceException {
        try {

            for (int i = 0; i < ids.length; i++) {
                baseDao.delete("user_activity", new Param("id", ids[i]));
            }
            return 1;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new ServiceException(e.getMessage(), e);

        }
    }

    public List queryActivityByUid(String uid) throws ServiceException {
        try {
            List result = baseDao.query("select * from user_activity where uid=:uid", new Param("uid", uid));
            return result;
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new ServiceException(e.getMessage(), e);
        }
    }

    public UserActivityModel getActivity(String id) throws ServiceException {
        try {
            List result = baseDao.query("select * from user_activity where id=:id", new Param("id", id));
            if (result.size() > 0) {
                Map map = (Map) result.get(0);
                UserActivityModel model = new UserActivityModel();
                model.setId(id);
                model.setUid(Util.getEmptyStr(map.get("uid")));
                model.setActivity(Util.getEmptyStr(map.get("activity")));
                model.setCreatetime(Util.getEmptyStr(map.get("createtime")));
                return model;
            } else
                return null;
        }

        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new ServiceException(e.getMessage(), e);
        }
    }
}
